package cronjob

import (
	"api_report/logconf"
	"api_report/readcf"
	"fmt"
	"github.com/robfig/cron/v3"
	"net"
	"os/exec"
	"strings"
	"time"
)

func GetPngCronJob(wscfg *readcf.WsCfg, logfile string) error {
	var logger = logconf.LogFormat(logfile)
	c := cron.New()
	spec := "*/10 * * * *"             // 间隔十分钟
	_, err := c.AddFunc(spec, func() { // 配置任务
		timer := time.Now().Format("200601021504")
		logconf.Logger.Info("start GetPngCronJob") // 开始执行任务
		for _, v := range wscfg.ServerList {
			cmdStr := fmt.Sprintf("/bin/bash ./shell/apigopng.sh %s %v %s", v.Host, v.Path, timer)
			_, err := net.DialTimeout("tcp", strings.Split(v.Host, "//")[1], 1*time.Second)
			if err != nil {
				logger.Error(v.Tag, err)
			} else {
				cmd := exec.Command("/bin/bash", "-c", cmdStr)
				_, err := cmd.Output()
				if err != nil {
					logger.Error(cmd.String(), err)
				}
			}
		}
	})
	if err != nil {
		logger.Fatal(err)
		return err
	}

	logger.Info("start main cron success")
	c.Start()
	return nil
	//select {}
}
